package com.zero.youxiao.service;

import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zero.basic.exception.SmsErrorConstant;
import com.zero.basic.exception.SmsRuntimeException;
import com.zero.basic.exception.SmsStatusCode;
import com.zero.basic.model.Pager;
import com.zero.basic.util.SimpleDateUtil;
import com.zero.mms.model.SalaryCalItem;
import com.zero.mms.model.SalaryItem;
import com.zero.youxiao.constant.SalaryItemConstant;
import com.zero.youxiao.dao.SalaryCalItemDao;
import com.zero.youxiao.dao.SalaryItemDao;

@Service("salaryItemService")
@Transactional(rollbackFor = { Exception.class, RuntimeException.class })
public class SalaryItemService {

	@Inject
	private SalaryItemDao salaryItemDao;

	@Inject
	private SalaryCalItemDao salaryCalItemDao;

	public List<SalaryItem> searchSalaryItems(String itemName, String itemType)
			throws SmsRuntimeException {
		try {
			if (itemType == null)
				itemType = "";
			if (itemName == null)
				itemName = "";
			List<SalaryItem> salaryItems = salaryItemDao.getSalaryItems(
					itemName, itemType);
			return salaryItems;
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,
					SmsErrorConstant.WEB_APPLICATION, "服务器错误");
		}
	}

	public Pager<SalaryItem> searchSalaryItemsByPage(String itemName,
			String itemType) throws SmsRuntimeException {
		try {
			if (itemType == null)
				itemType = "";
			if (itemName == null)
				itemName = "";
			Pager<SalaryItem> salaryItems = salaryItemDao.getSalaryItemsByPage(
					itemName, itemType);
			return salaryItems;
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,
					SmsErrorConstant.WEB_APPLICATION, "服务器错误");
		}
	}

	public SalaryItem loadItem(Integer itemId) throws SmsRuntimeException {
		try {
			if (itemId == null || "".equals(itemId))
				throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE,
						SmsErrorConstant.INVALID_AUGUMENT, "itemId不能为空");
			SalaryItem salaryItem = salaryItemDao.load(itemId);
			if (salaryItem.getSalaryItemType() != null) {
				if (salaryItem.getSalaryItemType().equals(
						SalaryItemConstant.Salary_cal)) {
					String foruma = salaryItem.getSalaryCal();
					System.out.println(foruma);
					String calPar1 = foruma.split("(\\+|-|\\*|/)")[0];
					String calPar2 = foruma.split("(\\+|-|\\*|/)")[1];
					Matcher matcher = Pattern.compile("(\\+|-|\\*|/)").matcher(
							foruma);
					matcher.find();
					String calOp = matcher.group();
					salaryItem.setCal1(calPar1);
					salaryItem.setCalOp(calOp);
					salaryItem.setCal2(calPar2);
				}
			}
			return salaryItem;
		} catch (SmsRuntimeException e) {
			throw e;
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,
					SmsErrorConstant.WEB_APPLICATION, "服务器错误");
		}
	}

	public void addSalaryItem(String itemName, String itemType,
			Integer lastOrder, Integer itemOrder, String itemAdd,
			String itemShow, String itemMemo, Integer itemCalItemId,
			String itemCalPar1, String itemOp, String itemCalPar2)
			throws SmsRuntimeException {
		try {
			if (itemName == null || "".equals(itemName) || itemType == null
					|| "".equals(itemType) || itemAdd == null
					|| "".equals(itemAdd) || itemShow == null
					|| "".equals(itemShow))
				throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE,
						SmsErrorConstant.INVALID_AUGUMENT, "参数不合法");
			SalaryItem salaryItem = new SalaryItem();
			if (itemShow.equals("是")) {
				if (lastOrder != itemOrder)
					salaryItemDao.updateAddAllOrder(itemOrder);
				salaryItem.setSalaryOrder(itemOrder);
			}
			if (itemType.equals(SalaryItemConstant.Salary_cal)) {
				if (itemCalPar1 == null || "".equals(itemCalPar1)
						|| itemCalPar2 == null || "".equals(itemCalPar2)
						|| itemOp == null || "".equals(itemOp))
					throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE,
							SmsErrorConstant.INVALID_AUGUMENT, "计算公式不能为空");
				if (!itemOp.matches("(\\+|-|\\*|/)"))
					throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE,
							SmsErrorConstant.INVALID_AUGUMENT, "公式操作符不合法");
				if (!itemCalPar2.matches("(\\d+\\.?\\d+|\\d+\\.?\\d+%)"))
					throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE,
							SmsErrorConstant.INVALID_AUGUMENT, "公式操作数不合法");
				String calGongshi = itemCalPar1 + itemOp + itemCalPar2;
				salaryItem.setSalaryCal(calGongshi);
			}
			salaryItem.setSalaryItemName(itemName);
			salaryItem.setSalaryItemType(itemType);
			salaryItem.setSalaryAddItem(itemAdd);
			salaryItem.setSalaryMemo(itemMemo);
			salaryItem.setShow(itemShow);
			salaryItemDao.add(salaryItem);
			if (itemType.equals(SalaryItemConstant.Salary_cal)) {
				SalaryCalItem salaryCalItem = new SalaryCalItem();
				salaryCalItem.setCalItem(itemCalItemId);
				salaryCalItem.setOperator(itemOp);
				if (itemCalPar2.matches("\\d+\\.?\\d+")) {
					salaryCalItem.setSeNum(Double.valueOf(itemCalPar2));
				} else {
					Matcher matcher = Pattern.compile("\\d+\\.?\\d+").matcher(
							itemCalPar2);
					matcher.find();
					String calString = matcher.group();
					double cal = Double.valueOf(calString) / 100;
					salaryCalItem.setSeNum(cal);
				}
				salaryCalItem.setSalaryItemId(salaryItem.getSalaryItemId());
				salaryCalItemDao.add(salaryCalItem);
			}
		} catch (SmsRuntimeException e) {
			throw e;
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,
					SmsErrorConstant.WEB_APPLICATION, "服务器错误");
		}
	}

	public void updateSalaryItem(Integer itemId, String itemName,String itemType, String itemAdd,
		String itemMemo,String itemShow, String itemOrder, String itemCal)
			throws SmsRuntimeException {
		try {
			if (itemName == null || "".equals(itemName) || itemType == null
					|| "".equals(itemType) || itemAdd == null
					|| "".equals(itemAdd) || itemShow == null
					|| "".equals(itemShow))
				throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE,
						SmsErrorConstant.INVALID_AUGUMENT, "参数不合法");
			SalaryItem salaryItem = salaryItemDao.load(itemId);
			if (itemShow.equals("是")) {
				if(itemOrder == null || "".equals(itemOrder))
					throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE,SmsErrorConstant.INVALID_AUGUMENT, "显示顺序不能为空");
				if(!itemOrder.matches(".\\d+."))
					throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE,SmsErrorConstant.INVALID_AUGUMENT, "显示顺序不合法");
				Matcher matcher = Pattern.compile("\\d+").matcher(itemOrder);
				matcher.find();
				Integer order = Integer.parseInt(matcher.group());
				if (salaryItem.getSalaryOrder() != order)
					salaryItemDao.updateOrder(order,salaryItem.getSalaryOrder());
				salaryItem.setSalaryOrder(order);
			}
			if (itemType.equals(SalaryItemConstant.Salary_cal)) {
				if (itemCal == null || "".equals(itemCal))
					throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE,SmsErrorConstant.INVALID_AUGUMENT, "计算公式不能为空");
				if(!itemCal.matches(".*(\\+|-|\\*|/)(\\d+\\.?\\d+|\\d+\\.?\\d+%)"))
					throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE,SmsErrorConstant.INVALID_AUGUMENT, "计算公式不合法");
				salaryItem.setSalaryCal(itemCal);
			}
			salaryItem.setSalaryItemName(itemName);
			salaryItem.setSalaryItemType(itemType);
			salaryItem.setSalaryAddItem(itemAdd);
			salaryItem.setSalaryMemo(itemMemo);
			salaryItem.setShow(itemShow);
			salaryItemDao.update(salaryItem);
			if (itemType.equals(SalaryItemConstant.Salary_cal)) {
				SalaryCalItem salaryCalItem = salaryCalItemDao.getByItemId(itemId);
				String itemCalPar1 = itemCal.split("(\\+|-|\\*|/)")[0];
				String itemCalPar2 = itemCal.split("(\\+|-|\\*|/)")[1];
				Matcher matcher = Pattern.compile("(\\+|-|\\*|/)").matcher(itemCal);
				matcher.find();
				String itemOp = matcher.group();
				salaryCalItem.setCalItem(salaryItemDao.getIdByName(itemCalPar1));
				salaryCalItem.setOperator(itemOp);
				if (itemCalPar2.matches("\\d+\\.?\\d+")) {
					salaryCalItem.setSeNum(Double.valueOf(itemCalPar2));
				} else {
					Matcher numMatcher = Pattern.compile("\\d+\\.?\\d+").matcher(
							itemCalPar2);
					numMatcher.find();
					String calString = numMatcher.group();
					double cal = Double.valueOf(calString) / 100;
					salaryCalItem.setSeNum(cal);
				}
				salaryCalItem.setCalDate(SimpleDateUtil.generateDateString(new Date()));
				salaryCalItem.setSalaryItemId(salaryItem.getSalaryItemId());
				salaryCalItemDao.update(salaryCalItem);
			}
		} catch(SmsRuntimeException e){
			throw e;
		}catch (Exception e) {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,
					SmsErrorConstant.WEB_APPLICATION, "服务器错误");
		}
	}

	public void deleteItem(Integer itemId) throws SmsRuntimeException {
		try {
			if ("".equals(itemId) || itemId == null)
				throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE,
						SmsErrorConstant.INVALID_AUGUMENT, "item不存在");
			SalaryItem salaryItem = salaryItemDao.load(itemId);
			salaryItemDao.updateAllOrder(salaryItem.getSalaryOrder());
			if (salaryItem.getSalaryCal() != null) {
				if (salaryItem.getSalaryCal().equals(
						SalaryItemConstant.Salary_cal)) {
					SalaryCalItem salaryCalItem = salaryCalItemDao
							.getByItemId(itemId);
					if (salaryCalItem != null)
						salaryCalItemDao.delete(salaryCalItem);
				}
			}
			salaryItemDao.delete(itemId);
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,
					SmsErrorConstant.WEB_APPLICATION, "服务器错误");
		}
	}

	public Integer getOrderList() throws SmsRuntimeException {
		try {
			return salaryItemDao.getOrderList().intValue() + 1;
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,
					SmsErrorConstant.WEB_APPLICATION, "服务器错误");
		}
	}

	public List<SalaryItem> getAllParSalaryItems() throws SmsRuntimeException {
		try {
			return salaryItemDao.getAllParItems();
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,
					SmsErrorConstant.WEB_APPLICATION, "服务器错误");
		}
	}

	public void updateSalaryItem(Integer itemId, String itemCal) throws SmsRuntimeException {
		try {
			if (itemId == null || "".equals(itemId))
				throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE,
						SmsErrorConstant.INVALID_AUGUMENT, "item不能玩为空");

			SalaryItem salaryItem = salaryItemDao.load(itemId);
			if(!itemCal.matches(".*(\\+|-|\\*|/)(\\d+\\.?\\d+|\\d+\\.?\\d+%)"))
				throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE,SmsErrorConstant.INVALID_AUGUMENT, "计算公式不合法");
			salaryItem.setSalaryCal(itemCal);
			salaryItemDao.update(salaryItem);
			
			SalaryCalItem salaryCalItem = salaryCalItemDao.getByItemId(itemId);
			String itemCalPar1 = itemCal.split("(\\+|-|\\*|/)")[0];
			String itemCalPar2 = itemCal.split("(\\+|-|\\*|/)")[1];
			Matcher matcher = Pattern.compile("(\\+|-|\\*|/)").matcher(itemCal);
			matcher.find();
			String itemOp = matcher.group();
			salaryCalItem.setCalItem(salaryItemDao.getIdByName(itemCalPar1));
			salaryCalItem.setOperator(itemOp);
			if (itemCalPar2.matches("\\d+\\.?\\d+")) {
				salaryCalItem.setSeNum(Double.valueOf(itemCalPar2));
			} else {
				Matcher numMatcher = Pattern.compile("\\d+\\.?\\d+").matcher(
						itemCalPar2);
				numMatcher.find();
				String calString = numMatcher.group();
				double cal = Double.valueOf(calString) / 100;
				salaryCalItem.setSeNum(cal);
			}
			salaryCalItem.setCalDate(SimpleDateUtil.generateDateString(new Date()));
			salaryCalItem.setSalaryItemId(salaryItem.getSalaryItemId());
			salaryCalItemDao.update(salaryCalItem);
		}catch(SmsRuntimeException e){
			throw e;
		}catch (Exception e) {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,
					SmsErrorConstant.WEB_APPLICATION, "服务器错误");
		}
	}

}
